﻿using System.Linq.Expressions;
using Basic.WxApiRemoteModel;
using Basic.WxModel.DB;
using Basic.WxModel.PlatformBind;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Basic.WxRepository.lmpl
{
    internal class WxPlatformBindRepository : WxBasicRepository<DBWxPlatformBind, long>, IWxPlatformBindRepository
    {
        public WxPlatformBindRepository (ISqlClientFactory factory) : base(factory)
        {
        }
        public Result[] GetBinds<Result> (string wxAppId, Expression<Func<DBWxPlatformBind, Result>> selector)
        {
            Result[] list = base.Gets(a => a.WxAppId == wxAppId && a.IsBind, selector);
            if (list.IsNull())
            {
                throw new ErrorException("wx.platform.bind.not.find");
            }
            return list;
        }
        public void Cancel (long id)
        {
            if (!base.Update(new
            {
                IsBind = false,
                CancelTime = DateTime.Now,
            }, a => a.Id == id))
            {
                throw new ErrorException("wx.platform.bind.update.fail");
            }
        }
        public long FindId (string appId, string wxAppId)
        {
            return base.Get<long>(c => c.AppId == appId && c.WxAppId == wxAppId, a => a.Id);
        }

        public T Find<T> (string appId, string wxAppId) where T : class, new()
        {
            return base.Get<T>(c => c.AppId == appId && c.WxAppId == wxAppId);
        }
        public void Add (WxPlatformBindAdd add)
        {
            DBWxPlatformBind bind = new DBWxPlatformBind
            {
                BindTime = DateTime.Now,
                IsBind = true,
                RefreshToken = add.RefreshToken,
                Id = IdentityHelper.CreateId(),
                WxAppId = add.WxAppId,
                AppId = add.AppId,
                WxPrower = add.WxPrower
            };
            base.Insert(bind);
        }
        public void Set (long id, string refreshToken, WxPrower[] prowers)
        {
            if (!base.Update(a => new DBWxPlatformBind
            {
                Id = id,
                IsBind = true,
                RefreshToken = refreshToken,
                BindTime = DateTime.Now,
                WxPrower = prowers
            }, new string[]
            {
                "IsBind",
                "RefreshToken",
                "BindTime",
                "WxPrower"
            }))
            {
                throw new ErrorException("wx.platform.update.fail");
            }
        }
        public void SetRefreshToken (long id, string refresh_token)
        {
            if (!base.Update(a => a.RefreshToken == refresh_token, a => a.Id == id))
            {
                throw new ErrorException("wx.platform.refreshToken.update.fail");
            }
        }

        public void SetPrower (long id, WxPrower[] prowers)
        {
            if (!base.Update(a => new DBWxPlatformBind
            {
                Id = id,
                WxPrower = prowers
            }, new string[]
            {
                "WxPrower"
            }))
            {
                throw new ErrorException("wx.platform.update.fail");
            }
        }
    }
}
